package com.examplemartin.jpademo.service.admin;


import com.examplemartin.jpademo.dao.MenuDao;
import com.examplemartin.jpademo.pojo.po.MenuPO;
import com.examplemartin.jpademo.pojo.vo.MenuVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
@Component
public class MenuService {
    @Resource
    private MenuDao menuDao;

    @Resource
    private OperatorLogService operatorLogService;

    // 按主键id查找菜单
    public MenuPO getMenuById(Long id) {
//    MenuPO allById = menuDao.findAllById(id);
        MenuPO menuPO = menuDao.findById(id).get();
        return menuPO;
    }

    // 查找所有菜单,包括一条虚构的顶级菜单
    public List<MenuPO> getMenuList() {
        List<MenuPO> all = menuDao.findAll();
        return all;
    }

    // 查找所有菜单按级别排序
    public List<MenuPO> getMenuListOrder() {
        ArrayList<MenuPO> all = new ArrayList<>();
        List<MenuPO> top = getTopMenulist();
        List<MenuPO> second = getSecondMenulist();
        for (MenuPO menuPO : top) {
            all.add(menuPO);
            for (MenuPO po : second) {
                if (menuPO.getId() == po.getParent().getId()) {
                    all.add(po);
                }
            }
        }

        return all;

    }

    // 查找所有菜单按级别排序--权限使用 父->>list子 结构
    public List<MenuVO> getMenuForRole() {
        List<MenuVO> all = new ArrayList<>();

        List<MenuPO> top = getTopMenulist();

        List<MenuPO> second = getSecondMenulist();


        for (MenuPO one : top) {

            MenuVO oneVO = new MenuVO();
            oneVO.setId(one.getId());
            oneVO.setMenuname(one.getMenuname());
            oneVO.setIcon(one.getIcon());
            oneVO.setUrl(one.getUrl());
            oneVO.setParent_id(one.getParent().getId());
            oneVO.setChild(new ArrayList());

            for (MenuPO two : second) {
                if (one.getId() == two.getParent().getId()) {
                    MenuVO twoVO = new MenuVO();
                    twoVO.setId(two.getId());
                    twoVO.setMenuname(two.getMenuname());
                    twoVO.setIcon(two.getIcon());
                    twoVO.setUrl(two.getUrl());
                    twoVO.setParent_id(two.getParent().getId());
                    oneVO.getChild().add(twoVO);
                }

            }

            all.add(oneVO);
        }

        return all;

    }


    // 按上级菜单查找 -- 这里简化为只有2级菜单，所以直接查找顶级菜单
    public List<MenuPO> getMenuByParent() {
        MenuPO parent = menuDao.findById(1L).get();
        List<MenuPO> all = menuDao.findMenuPOSByParent(parent);
        return all;
    }

    public List<MenuPO> getMenuByParent(MenuPO parent) {

        List<MenuPO> all = menuDao.findMenuPOSByParent(parent);
        return all;
    }

    // 添加菜单
    public MenuPO insertMenuPO(MenuPO menuPO) {
        menuPO.setCreateTime(new Date());

        MenuPO save = menuDao.save(menuPO);

        return save;


    }

    public List<MenuPO> getTopMenulist() {
        List<MenuPO> top = new ArrayList<>();
        List<MenuPO> all = menuDao.findAll();
        all.sort(new Comparator<MenuPO>() {
            @Override
            public int compare(MenuPO o1, MenuPO o2) {
                if (o1.getPosition() == o2.getPosition()) {
                    if (o1.getId() - o2.getId() > 0) return 2 - 1;
                    if (o1.getId() - o2.getId() < 0) return 1 - 2;
                    if (o1.getId() - o2.getId() == 0) return 1 - 1;
                }
                return o1.getPosition() - o2.getPosition();
            }
        });
        for (MenuPO menuPO : all) {
            if (menuPO.getId() != 1l && menuPO.getParent().getId() == 1) {
                top.add(menuPO);
            }
        }
        return top;
    }

    public List<MenuPO> getSecondMenulist() {
        List<MenuPO> second = new ArrayList<>();
        List<MenuPO> all = menuDao.findAll();

        all.sort(new Comparator<MenuPO>() {
            @Override
            public int compare(MenuPO o1, MenuPO o2) {
                if (o1.getPosition() == o2.getPosition()) {
                    if (o1.getId() - o2.getId() > 0) return 2 - 1;
                    if (o1.getId() - o2.getId() < 0) return 1 - 2;
                    if (o1.getId() - o2.getId() == 0) return 1 - 1;
                }
                return o1.getPosition() - o2.getPosition();
            }
        });

        for (MenuPO menuPO : all) {
            if (menuPO.getId() != 1l && menuPO.getParent().getId() != 1) {
                second.add(menuPO);
            }
        }

        return second;
    }

    public MenuPO saveMenuPO(MenuPO menuPO) {

        menuPO.setUpdateTime(new Date());

        MenuPO save = menuDao.save(menuPO);

        return save;
    }

    public MenuPO removeMenu(Long id) {
        MenuPO menuPO = menuDao.findById(id).get();
        if (menuPO == null) {
            return null;
        } else {
            menuDao.delete(menuPO);
            return menuPO;
        }

    }


}
